define(['jquery', 'bootstrap', 'frontend', 'form', 'template', 'cookie', 'frontend/xiao_man_yao', 'frontend/whiteboard', 'frontend/loginpop', 'frontend/password_verified'], function ($, undefined, Frontend, Form, Template, cookie_class, XiaoManYao, WhiteBoard, LoginPop, PasswordVerified) {

    var shou_obj = null;
    var ting_obj = null;
    var subject_class_id = 0;
    var attachement_id = 0;
    var unique_id = 0;
    var player = null;
    var Controller = {
        index: function () {

            //初始化密码验证
            PasswordVerified.init();

            //初始化黑板
            WhiteBoard.init();

            //初始化小蛮妖
            XiaoManYao.init();

            //从地址栏带过来的参数，自动展开对应的科目
            if (Config.unique_id != "") {
                unique_id = Config.unique_id;
                //当前节点
                subject_class_id = $(".subject-class-body [unique_id='" + unique_id + "']").attr("id");
                console.log("subject_class_id", subject_class_id);
                $(".subject-class-body [id='" + subject_class_id + "']").addClass("menu-focus");
                $(".subject-class-body [id='" + subject_class_id + "']").data("status", "open");

                //向下箭头，向下的列表
                $(".subject-class-body [dropdown-unique-id='" + unique_id + "']").addClass("rotate");
                $(".subject-class-body [id='menu-1-items-" + subject_class_id + "']").show();
                $(".subject-class-body [id='menu-items-" + subject_class_id + "']").show();

                //向上的元素
                $(".subject-class-body [unique_id='" + unique_id + "']").parentsUntil(".subject-class-body  .main-menu-body").show();
                $(".subject-class-body [unique_id='" + unique_id + "']").parentsUntil(".subject-class-body  .main-menu-body").each(function () {
                    var top_dropdown = $(this).attr("top-dropdown");
                    if (top_dropdown !== undefined) {
                        console.log(top_dropdown);
                        $("#" + top_dropdown).addClass("rotate");

                        //顶级元素
                        var top_menu_id = $(this).attr("top_menu_id");
                        $(".subject-class-body [id='" + top_menu_id + "']").data("status", "open");
                    }
                });

                Controller.fetchSubjectAttachment();
            }

            $(".subject-class-body .menu-1-title").on("click", function () {

                var unique_id = $(this).attr("unique_id");
                var url= $(this).attr("url");
                if(url!=""){
                    window.location=url;
                }else{
                    window.location="/index/classroom/index?unique_id="+unique_id;
                }
                return false;

                // var id = $(this).attr("id");
                // //关闭同级其他一级菜单
                // $(".subject-class-body .menu-1-title").each(function () {
                //     var id2 = $(this).attr("id");
                //     if (id != id2) {
                //         $(this).removeClass("menu-focus");
                //         $(".subject-class-body #menu-1-items-" + id2).css("display", "none");
                //         $(".subject-class-body #menu-1-dropdown-" + id2).removeClass("rotate");
                //     }
                // });

                // if ($(this).hasClass("menu-focus")) {
                //     $(this).removeClass("menu-focus");
                //     subject_class_id = 0;
                //     $(".subject-class-body #menu-1-items-" + id).css("display", "none");
                //     $(".subject-class-body #menu-1-dropdown-" + id).removeClass("rotate");
                // } else {
                //     $(this).addClass("menu-focus");
                //     subject_class_id = id;
                //     $(".subject-class-body #menu-1-items-" + id).css("display", "block");
                //     $(".subject-class-body #menu-1-dropdown-" + id).addClass("rotate");

                // }

                // console.log("subject_class_id", subject_class_id);

                // WhiteBoard.subject_class_id = subject_class_id;
                // WhiteBoard.autoLoadWhiteBoardImage();

                // page = 1;
                // Controller.fetchSubjectAttachment();
                // return false;
            });

            $(".subject-class-body .menu-title").on("click", function () {

                var unique_id = $(this).attr("unique_id");
                var url= $(this).attr("url");
                if(url!=""){
                    window.location=url;
                }else{
                    window.location="/index/classroom/index?unique_id="+unique_id;
                }
                return false;

                // var id = $(this).attr("id");
                // $(".subject-class-body .menu-title").removeClass("menu-focus");
                // $(this).addClass("menu-focus");
                // $(".subject-class-body #menu-items-" + id).css("display") == "block" ? $(".subject-class-body  #menu-items-" + id).css("display", "none") : $(".subject-class-body  #menu-items-" + id).css("display", "block");

                // subject_class_id = id;
                // console.log("subject_class_id", subject_class_id);

                // WhiteBoard.subject_class_id = subject_class_id;
                // WhiteBoard.autoLoadWhiteBoardImage();

                // page = 1;
                // Controller.fetchSubjectAttachment();
                // return false;
            });


            //显示用户头像
            if(LoginPop.logined==1){
                $(".user-info .avatar").attr("src",Config.user_info.avatar);
                $(".user-info").show();
            }
        },
        fetchSubjectAttachment: function () {
            shou_obj = null;
            ting_obj = null;

            //去除点击事件
            $(".shou").off('click');
            $(".ting").off('click');

            $.ajax({
                url: '/index/classroom/fetchSubjectAttachment', // 服务器端的URL
                type: 'post',
                data: { subject_class_id: subject_class_id }, // 发送到服务器的数据
                success: function (response) {
                    console.log(response);
                    var arr = response;
                    var text2 = '';
                    //换行计数器，每两个换一行
                    var j = 0;
                    for (var i = 0; i < arr.length; i++) {
                        //授
                        if (arr[i].show_position == "2") {
                            shou_obj = arr[i];
                        }
                        //听
                        else if (arr[i].show_position == "3") {
                            ting_obj = arr[i];
                        } else {
                            //右侧栏
                            if (j % 2 == 0) {
                                text2 += '<div class="file-item-row">';
                            }
                            j++;

                            var str2 = Controller.thumb(arr[i]);
                            text2 += str2;

                            if (j % 2 == 0) {
                                text2 += '</div>';
                            }
                        }
                    }
                    $(".file-item-rows").html(text2);

                    $(".shou").removeClass("shou-focus");
                    if (shou_obj) {
                        $(".shou").addClass("shou-focus");
                    }
                    $(".ting").removeClass("ting-focus");
                    if (ting_obj) {
                        $(".ting").addClass("ting-focus");
                    }

                    $(".file-item").on('click', function () {
                        //如果没有登陆,就弹出登陆窗口
                        if (LoginPop.logined == 0) {
                            LoginPop.openLoginWindow()
                            return false;
                        }

                        $(".file-item").removeClass("file-item-focus");
                        $(this).addClass("file-item-focus");

                        var url = $(this).attr("url");
                        var imagetype = $(this).attr("imagetype");
                        var filename = $(this).attr("filename");
                        var shou = $(this).attr("shou");
                        var ting = $(this).attr("ting");
                        var id = attachement_id = $(this).attr("id");
                        var unique_id = $(this).attr("unique_id");

                        var row = {
                            "id": id,
                            "unique_id": unique_id,
                            "url": url,
                            "imagetype": imagetype,
                            "filename": filename,
                            "shou": shou,
                            "ting": ting
                        };

                        Controller.openFile(row);
                        return false;
                    });

                    $(".shou").on('click', function () {
                        //如果没有登陆,就弹出登陆窗口
                        if (LoginPop.logined == 0) {
                            LoginPop.openLoginWindow()
                            return false;
                        }

                        if (shou_obj != null) {
                            attachement_id = shou_obj.id;
                            Controller.openFile(shou_obj);
                        }
                        return false;
                    });

                    $(".ting").on('click', function () {

                        //如果没有登陆,就弹出登陆窗口
                        if (LoginPop.logined == 0) {
                            LoginPop.openLoginWindow()
                            return false;
                        }

                        if (ting_obj != null) {
                            attachement_id = ting_obj.id;
                            Controller.openFile(ting_obj);
                        }
                        return false;
                    });
                }
            });
        },
        openFile: function (row) {

            //更新浏览量
            $.ajax({
                url: '/index/classroom/view',
                type: 'post',
                data: { id: row.unique_id },
                success: function (response) { }
            });


            if (row.imagetype == "png" || row.imagetype == "jpg" || row.imagetype == "jpeg" || row.imagetype == "gif") {
                Controller.showImage(row);

                //加载黑板
                WhiteBoard.subject_class_id = subject_class_id;
                WhiteBoard.attachement_id = attachement_id;
                WhiteBoard.openWhiteboardWindow();
                return;
            }

            if (row.imagetype == "pdf") {
                Controller.showPDF(row);

                //加载黑板
                WhiteBoard.subject_class_id = subject_class_id;
                WhiteBoard.attachement_id = attachement_id;
                WhiteBoard.openWhiteboardWindow();
                return;
            }

            if (row.imagetype == "pptx" || row.imagetype == "pptx" || row.imagetype == "doc" || row.imagetype == "docx" || row.imagetype == "xls" || row.imagetype == "xlsx") {
                Controller.showOffice(row);

                //加载黑板
                WhiteBoard.subject_class_id = subject_class_id;
                WhiteBoard.attachement_id = attachement_id;
                WhiteBoard.openWhiteboardWindow();
                return;
            }

            if (row.imagetype == "mp4") {
                Controller.showMP4(row);

                //加载黑板
                WhiteBoard.subject_class_id = subject_class_id;
                WhiteBoard.attachement_id = attachement_id;
                WhiteBoard.openWhiteboardWindow();
                return;
            }

            window.open(row.url);
            return;
        },
        thumb: function (row) {
            var html = '';
            if (row.mimetype.indexOf("image") > -1) {
                html = ' <div class="file-item" id="' + row.id + '" unique_id="' + row.unique_id + '" title="' + row.filename + '"  url="' + row.url + '" imagetype=' + row.imagetype + ' filename=' + row.filename + ' shou=' + row.shou + ' ting=' + row.ting + ' >';
                html += '  <div class="file-item-icon"><img src="' + row.url + '" ></div>';
                html += '  <div class="file-item-title">' + row.filename + '</div>';
                html += '</div>';
            } else {
                html = ' <div class="file-item" id="' + row.id + '" unique_id="' + row.unique_id + '" title="' + row.filename + '" url="' + row.url + '" imagetype=' + row.imagetype + ' filename=' + row.filename + ' shou=' + row.shou + ' ting=' + row.ting + ' >';
                html += '  <div class="file-item-icon"><img src="' + Fast.api.fixurl("/index/ajax/icon") + "?suffix=" + row.imagetype + '"></div>';
                html += '  <div class="file-item-title">' + row.filename + '</div>';
                html += '</div>';
            }
            return html;
        },
        showMP4: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "none");

            var str = '<video-js id="fullscreen_video"  controls preload="auto"  ><source src="' + url + '" type="video/mp4"></source></video-js>';
            // var str = '<video id="fullscreen_video"  controls   ><source src="' + url + '" type="video/mp4"> 您的浏览器不支持HTML5视频。</video>';

            $(".mid-body-top").html(str);

            if (player != null) {
                player.dispose();
            }

            player = videojs('fullscreen_video', {
                playbackRates: [0.5, 1, 1.5, 2]
            });

            player.ready(function () {
                console.log("player ready");
                player.volume(0.2);

                var playbackTime = $.cookie(url);
                var duration = $.cookie(url + "_duration");
                console.log("playbackTime", playbackTime);
                console.log("duration", duration);

                if (parseFloat(playbackTime) < parseFloat(duration)) {
                    console.log("last played did not finished");
                    player.currentTime(parseFloat(playbackTime));
                } else {
                    console.log("no played or finished");
                }
            });

            var playing = false;

            // 监听播放事件
            player.on('play', function () {
                console.log('Video is playing');
                playing = true;
            });

            // 监听暂停事件
            player.on('pause', function () {
                console.log('Video is paused');
                playing = false;
            });

            // 监听timeupdate事件
            player.on('timeupdate', function () {
                if (playing) {
                    console.log("timeupdate");

                    var playbackTime = player.currentTime(); // 获取当前播放时间
                    console.log("playbackTime", playbackTime);

                    var duration = player.duration(); // 获取视频总时长
                    console.log("duration", duration);

                    $.cookie(url + "_duration", duration + "", { expires: 365, path: "/" });
                    //没有播完
                    if (playbackTime < duration) {
                        $.cookie(url, playbackTime + "", { expires: 365, path: "/" });
                    } else {
                        $.cookie(url, null);
                        $.cookie(url + "_duration", null);
                    }
                }
            });

            const playerContainer = document.getElementById('fullscreen_video');
            playerContainer.oncontextmenu = function (event) {
                // 阻止默认的右键菜单显示
                event.preventDefault();
            };

        },
        showImage: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "none");

            var str = '<img id="fullscreen_img"  src="' + url + '"  />';
            $(".mid-body-top").html(str);

            $("#fullscreen_img").click(function () {
                Controller.allScreen("fullscreen_img");
            });

        },
        showPDF: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "block");


            // var str = '<iframe  id="fullscreen_pdf"  src="/pdfj/web/viewer.html?file=' + url + '" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"  ></iframe>';
            //     $(".mid-body-top").html(str);

            PDFObject.embed(url, ".mid-body-top");

            $(".loading").css("display", "none");


            // $('#fullscreen_pdf').on('load', function () {
            //     // iframe加载完成后的操作
            //     console.log('iframe加载完成');
            //     $(".loading").css("display", "none");

            // });
        },
        showOffice: function (row) {

            var url = row.url;
            var filename = row.filename;

            $(".loading").css("display", "block");

            // var url1 = "http://feng.eucms.net/classroom" + url + "&wdAr=1.7777777777777777";
            var url1 = "http://8.134.104.50:8088" + url;
            var url2 = encodeURI(url1);
            // var str = '<iframe id="my-iframe"  src="https://view.officeapps.live.com/op/embed.aspx?src=' + url2 + '" >正在加载文档，请等待。。。。。</iframe>';
            var str = '<iframe id="fullscreen_office"   src="https://vip.ow365.cn/?i=34482&furl=' + url2 + '" >正在加载文档，请等待。。。。。</iframe>';
            $(".mid-body-top").html(str);

            $('#fullscreen_office').on('load', function () {
                // iframe加载完成后的操作
                console.log('iframe加载完成');
                $(".loading").css("display", "none");
            });
        },
        allScreen: function (object_id) {
            var doc = document.getElementById(object_id);
            if (doc.requestFullscreen) {
                doc.requestFullscreen();
            } else if (doc.webkitRequestFullScreen) {
                doc.webkitRequestFullScreen();
            } else if (doc.mozRequestFullScreen) {
                doc.mozRequestFullScreen();
            } else if (doc.msRequestFullscreen) {
                // IE11
                doc.msRequestFullscreen();
            }
        },
        exitFullscreen: function () {
            let doc = document;
            if (doc.exitFullscreen) {
                doc.exitFullscreen();
            } else if (doc.webkitCancelFullScreen) {
                doc.webkitCancelFullScreen();
            } else if (doc.mozCancelFullScreen) {
                doc.mozCancelFullScreen();
            } else if (doc.msExitFullscreen) {
                doc.msExitFullscreen();
            }
        },

    };
    return Controller;
});
